package com.cndw.kungfu.business.gm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cndw.commons.util.BlankUtils;
import com.cndw.kungfu.comm.GmCommand;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.JsonProcesser;
import com.cndw.kungfu.ext.UtilsOfBusiness;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.general.ModeLog;
import com.cndw.kungfu.model.sys.LogManager;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.annotation.UnAuth;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

@Controller
public class BusinessLog {

	@Autowired
	private LogManager logManager;

	@UnAuth
	@GmCommand
	public ResponseMessage getTemplate(Session session, String sign) {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 一级类型
		Map<Integer, String> typeMap = new HashMap<Integer, String>();
		typeMap.put(ConstantsLog.forge, "装备");
		typeMap.put(ConstantsLog.task, "任务");
		typeMap.put(ConstantsLog.player, "人物");
		typeMap.put(ConstantsLog.battle, "战斗");
		typeMap.put(ConstantsLog.gold, "元宝");
		typeMap.put(ConstantsLog.union, "帮派");
		typeMap.put(ConstantsLog.goods, "道具");
		typeMap.put(ConstantsLog.disciple, "门徒");
		typeMap.put(ConstantsLog.activity, "活动");
		typeMap.put(ConstantsLog.playerExt11, "人物扩展");
		typeMap.put(ConstantsLog.actWeek12, "节日活动");
		typeMap.put(ConstantsLog.marry, "结婚");
		typeMap.put(ConstantsLog.actNew15, "新长期活动");
		
		resultMap.put("type", typeMap);

		// 二级类型
		Map<Integer, List<String>> eventMap = new HashMap<Integer, List<String>>();
		for (ModeLog log : ModeLog.getLoglist()) {
			if (!eventMap.containsKey(log.getObjectId())) {
				eventMap.put(log.getObjectId(), new ArrayList<String>());
			}

			try {
				eventMap.get(log.getObjectId()).add(JsonProcesser.objectJsonEncode(log));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		resultMap.put("event", eventMap);

		return ResponseMessage.getSuccMessage(resultMap);
	}

	@UnAuth
	@GmCommand
	public ResponseMessage getLogs(Session session, String sign, Map<String, Object> paramMap) {

		int style = Integer.parseInt(paramMap.get("type").toString());
		String user = (String) paramMap.get("player");
		long playerId = UtilsOfBusiness.getPlayerId(style, user);
		String param = BlankUtils.isBlank(paramMap.get("param")) ? null : paramMap.get("param").toString();
		String ip = BlankUtils.isBlank(paramMap.get("ip")) ? null : paramMap.get("ip").toString();

		int objectId = Integer.parseInt(paramMap.get("objectId").toString());
		int eventId = Integer.parseInt(paramMap.get("eventId").toString());
		int beginTime = Integer.parseInt(paramMap.get("beginTime").toString());
		int endTime = Integer.parseInt(paramMap.get("endTime").toString());
		int pageSize = Integer.parseInt(paramMap.get("pageSize").toString());
		int pageCount = Integer.parseInt(paramMap.get("pageCount").toString());
		pageCount = pageCount == 0 ? 1 : pageCount;

		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("logs", logManager.getLogs(playerId, objectId, eventId, param, beginTime, endTime, (pageCount - 1) * pageSize, pageSize,ip));
		resultMap.put("totals", logManager.getLogCounts(playerId, objectId, eventId, param, beginTime, endTime,ip));

		Player player = Context.getPlayerServer().getByPlayerId(playerId);
		if (!BlankUtils.isBlank(player)) {
			resultMap.put("playerAccount", player.getAccountName());
			resultMap.put("playerName", player.getPlayerName());
		}
		return ResponseMessage.getSuccMessage(resultMap);
	}
}
